package com.surfilter.platform.base.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.surfilter.platform.base.exception.DaoException;
import com.surfilter.platform.base.model.PageInfo;
import com.surfilter.platform.base.util.ObjectUtil;

public abstract class DaoBase<K> extends SqlMapClientDaoSupport {

	protected Object addObject(String statementName, Object object) {
		try {			
			Object key = this.getSqlMapClientTemplate().insert(statementName,
					object);
			return key;
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	protected List<K> getObjectsByObject(String statementName, Object object) {
		try {
			return this.getSqlMapClientTemplate().queryForList(statementName,
					object);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	protected List getListByObject(String statementName, Object object) {
		try {
			return this.getSqlMapClientTemplate().queryForList(statementName,
					object);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	protected List<K> getObjectsByObjectForPage(String statementName,
			Object object, PageInfo pageInfo) {
		try {
			Map params = null;
			if (object != null) {
				params = ObjectUtil.objectToMap(object);
				if (params == null) {
					params = new HashMap();
					params.put("object_value", object);
				}
			}
			if(pageInfo!=null){
				Map pageParams = ObjectUtil.objectToMap(pageInfo);
				if(pageParams!=null)
					params.putAll(pageParams);
			}
			return this.getSqlMapClientTemplate().queryForList(statementName,
					params);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	protected List getListByObjectForPage(String statementName, Object object,
			PageInfo pageInfo) {
		try {
			Map params = null;
			if (object != null) {
				params = ObjectUtil.objectToMap(object);
				if (params == null) {
					params = new HashMap();
					params.put("object_value", object);
				}
			}
			if(pageInfo!=null){
				Map pageParams = ObjectUtil.objectToMap(pageInfo);
				if(pageParams!=null)
					params.putAll(pageParams);
			}
			return this.getSqlMapClientTemplate().queryForList(statementName,
					params);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	protected Object getObjectByObject(String statementName, Object object) {
		try {
			return this.getSqlMapClientTemplate().queryForObject(statementName,
					object);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	protected int updateObject(String statementName, Object object) {
		try {
			return this.getSqlMapClientTemplate().update(statementName, object);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	protected int deleteObject(String statementName, Object object) {
		try {
			return this.getSqlMapClientTemplate().delete(statementName, object);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	protected Map getMapByObject(String statementName, Object object,
			String keyName, String valueName) {
		try {
			return this.getSqlMapClientTemplate().queryForMap(statementName,
					object, keyName, valueName);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	@SuppressWarnings("unchecked")
	protected void updateBatch(final String statementName, final List list){
		try {
			if(list != null){
				this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {

					@Override
					public Object doInSqlMapClient(SqlMapExecutor executor)
							throws SQLException {
						// TODO Auto-generated method stub
						executor.startBatch();
						for(int i = 0, n = list.size(); i < n; i++){
							executor.update(statementName, list.get(i));
						}
						executor.executeBatch();
						return null;
					}
				});
			}
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	protected void addBatch(final String statementName, final List list){
		try {
			if(list != null){
				this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
					
					@Override
					public Object doInSqlMapClient(SqlMapExecutor executor)
							throws SQLException {
						executor.startBatch();
						for(int i = 0, n = list.size(); i < n; i++){
							executor.insert(statementName, list.get(i));
						}
						executor.executeBatch();
						return null;
					}
				});
			}
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	protected void deleteBatch(final String statementName, final List list){
		try {
			if(list != null){
				this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
					
					@Override
					public Object doInSqlMapClient(SqlMapExecutor executor)
							throws SQLException {
						executor.startBatch();
						for(int i = 0, n = list.size(); i < n; i++){
							executor.delete(statementName, list.get(i));
						}
						executor.executeBatch();
						return null;
					}
				});
			}
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	@PostConstruct
	public abstract void initSqlMapClient();

}
